package com.itheima.ssm.dao;

import com.itheima.ssm.domain.Pemission;
import com.itheima.ssm.domain.Role;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * Role的Dao接口
 */
public interface IRoleDao {

    /**
     * 通过userId找到role的信息，返回Role的list集合
     * 数据库中user和role的中间表为users_role,通过此表关联两张表的主键
     * 内嵌子查询
     * @param userId
     * @return
     */
    @Select("select * from role where id in (select roleId from users_role where id = #{userId})")
    @Results({
            // 封装Pemession对象的信息
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "roleName", column = "roleName"),
            @Result(property = "roleDesc", column = "roleDesc"),
            @Result(property = "permissions",column = "id",javaType = java.util.List.class,many = @Many(select = "cn.itcast.ssm.dao.PermissionDao.findPermissionByRoleId"))
    })
    public List<Role> findRoleByUserId(int userId);


    @Select("select * from role where id in (select roleId from role_permession where id = #{permesionId})")
    @Results({
            // 由于role封装这userinfo，这里我们也需要获取
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "username",column = "username"),
            @Result(property = "email",column = "email"),
            @Result(property = "password",column = "password"),
            @Result(property = "phoneNum",column = "phoneNum"),
            @Result(property = "status",column = "status"),
    })
    public List<Role> findRoleByPermessionId(int permessionId);


    /**
     * 查询所有角色
     * @return
     * @throws Exception
     */
    @Select("select * from role")
    List<Role> findAll() throws Exception;

    /**
     * 添加角色
     * @param role
     * @throws Exception
     */
    @Insert("insert into role(roleName,roleDesc) values(#{roleName},#{roleDesc})")
    void save(Role role) throws Exception;


    /**
     * 根据roleid删除角色
     * 由于role关联userInfo，关联permission两张表，所以删除的时候主要将关联表中对应的信息都需要删除
     * @param roleId
     */
    @Delete("delete from role where id = #{roleId}")
    public void deleteRole(int roleId);

    /**
     * 根据roleId删除关联表中数据
     * @param roleId
     */
    @Delete("delete from users_role where id = #{roleId}")
    public void deleteUserIdByRoleId(int roleId);

    @Delete("delete from role_permession where id = #{roleId}")
    public void deletePermissionByRoleId(int roleId);

    @Select("select * from role where id = #{roleId}")
    Role findById(String roleId) throws Exception;

    @Select("select * from permission where id not in (select permissionId from role_permission where id = #{roleId})")
    List<Pemission> findOtherPermission(String roleId) throws Exception;

    /**
     * Mybatis的@Param注解的作用，我个人感觉就俩作用：1.便于传多个参数；2.类似于别名之类的功能
     * @param roleId
     * @param pid
     * @throws Exception
     */
    @Insert("insert into role_permission(roleId,permissionId) values(#{roleId},#{permissionId})")
    void addPermissionToRole(@Param("roleId") String roleId, @Param("permissionId") String pid) throws Exception;
}
